package com.smallcr.doc;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.smallcr.constant.HttpStatusEnum;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration //声明该类为配置类
@EnableSwagger2 //声明启动Swagger2
@EnableWebMvc //声明启动mvc
public class SwaggerConfig {
	public final static String contentType = MediaType.APPLICATION_JSON_VALUE;;
	
	public final static String protocol = "http";
	
	/**是否启用swagger **/
	@Value("${config.swagger.enabled:false}")
	private boolean swaggerShow;
	
	@Value("${config.swagger.applicationName}")
	private String applicationName;
	
	/**扫描目录 **/
	@Value("${config.swagger.basePackage:com.smallcr}")
	private String basePackage;
	    
		@Bean
		public Docket defaultAPI() {
			
			List<Parameter> aParameters = Lists.newArrayList();
			ParameterBuilder aParameterBuilder = new ParameterBuilder();
			
//			//给教师端加上头部
//			if("smallcr-teacher".equals(applicationName)) {
//				
//				aParameterBuilder.name("X-SMALLCR-APPCODE").description("应用码:smallcr-teacher").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
//				aParameters.add(aParameterBuilder.build());
//				
//				aParameterBuilder.name("X-SMALLCR-SESSION").description("会话Token").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
//				aParameters.add(aParameterBuilder.build());
//			}
//			//给家长端加上头部
//			if("smallcr-pupil".equals(applicationName)) {
//				
//				aParameterBuilder.name("X-SMALLCR-APPCODE").description("应用码:smallcr-pupil").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
//				aParameters.add(aParameterBuilder.build());
//				
//				aParameterBuilder.name("X-SMALLCR-SESSION").description("会话Token").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
//				aParameters.add(aParameterBuilder.build());
//			}
			
			ApiInfo apiInfo = new ApiInfoBuilder()
					.title(applicationName+"接口文档")
					.description("包含"+applicationName+" App所有的接口定义，使用Swagger2构建RESTful API")
					.version("3.0.0").build();
			return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).select()
					.apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any()).build()
					.useDefaultResponseMessages(false).globalResponseMessage(RequestMethod.GET, customerResponseMessage())
					.globalOperationParameters(aParameters)
					.globalResponseMessage(RequestMethod.POST, customerResponseMessage())
					.globalResponseMessage(RequestMethod.PUT, customerResponseMessage())
					.globalResponseMessage(RequestMethod.DELETE, customerResponseMessage()).forCodeGeneration(true)
					.protocols(Sets.newHashSet(protocol)).produces(Sets.newHashSet(contentType)).groupName("ALL")
					.enable(swaggerShow);
		}

		/**
		 * @Title: customerResponseMessage
		 * @Description: 自定义responseMessage
		 * @return List<ResponseMessage>    返回类型
		 * @throws
		 */
		public List<ResponseMessage> customerResponseMessage() {
			List<ResponseMessage> responseMessages = Lists.newArrayList();
			// 500
			responseMessages.add(new ResponseMessageBuilder().code(Integer.valueOf(HttpStatusEnum.SERVER_ERROR.getCode()))
					.responseModel(new ModelRef("Error")).message(HttpStatusEnum.SERVER_ERROR.getMsg()).build());
			// 405
			responseMessages.add(new ResponseMessageBuilder().code(Integer.valueOf(HttpStatusEnum.NOT_ALLOWED.getCode()))
					.responseModel(new ModelRef("Error")).message(HttpStatusEnum.NOT_ALLOWED.getMsg()).build());
			// 400
			responseMessages.add(new ResponseMessageBuilder().code(Integer.valueOf(HttpStatusEnum.BAD_REQUEST.getCode()))
					.message(HttpStatusEnum.BAD_REQUEST.getMsg()).responseModel(new ModelRef("Error")).build());
			// 401
			responseMessages.add(new ResponseMessageBuilder().code(Integer.valueOf(HttpStatusEnum.UNAUTHORIZED.getCode()))
					.message(HttpStatusEnum.UNAUTHORIZED.getMsg()).responseModel(new ModelRef("Error")).build());
			// 505
			responseMessages.add(new ResponseMessageBuilder().code(Integer.valueOf(HttpStatusEnum.SERVER_FAIL.getCode()))
					.responseModel(new ModelRef("Error")).message(HttpStatusEnum.SERVER_FAIL.getMsg()).build());

			return responseMessages;
		}
}
